<?php

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ReceivingAccountController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'seller/ejbao'],
        'show' => ['get', 'seller/ejbao/%d'],
        'store' => ['post', 'seller/ejbao'],
        'update' => ['put', 'seller/ejbao/%d'],
        'put' => ['put', ''],
        'destroy' => ['delete', 'seller/ejbao/%d'],
        'update_default'=>['post', 'seller/ejbao/update_default'],
        'default_account'=>['get', 'seller/ejbao/default_account'],
        'account_sum'=>['get', 'seller/company/summery'],
        'check_pay_pwd'=>['get', 'user/checkpaypwd'],
        'last_login'=>['get','user/lastlogin'],
        'czbank_bind_receipt'=>['post','bindReceipt'],
        'czbank_send_sms'=>['post','sendMessage'],
        'czbank_query_bank'=>['post','queryBank'],
        'company_info'=>['get','seller/companymaterial/%d/showusercompany'],
        
    ];

    protected $shop_id;
    protected $user_id;
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function index(){
        //参数部分
        $search_params = $this->request->all(['e_id']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'orderBy'=>'id',
            'sortedBy'=>'desc',
        ];
        
        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);
     
        if(!isset($ret['data']['list'][0])){
            $info = http_web($this->api_url['company_info'],[session('user_id')])->send();
            if(isset($info['data']['data']['company'])){
                $company = $info['data']['data']['company'];
                $data['accout_status'] = 0;
                $data['account_name'] = $company['company_name'];
                $data['bank_no'] = $company['bank_account'];
                $data['account_type']="1";
                $data['bank_name']=$company['bank'];
                $data['bank_phone'] = isset($info['data']['data']['phone']) ? $info['data']['data']['phone'] : "" ;
                
                $banks = config('bank');
                $bank_en = array_search($data['bank_name'], $banks);
                $data['account_icon']="https://apimg.alipay.com/combo.png?d=cashier&t=".$bank_en;
                $data['account_en'] =$bank_en;
                
                http_web($this->api_url['store'])->send($data);
            }
            $ret = http_web($this->api_url['index'])->send($params);
        }
        
        //结果处理部分
        $data = paginate($this->request, $ret['data']);
        //回填搜索框值
        $data['search_params'] = $search_params;
    
        return view('web.user.seller.receiving_accounts', $data);
    }
    public function ejbao()
    {
        
        $params = [
        ];

        //请求部分
        $data=['unreceive_money'=>0.00,'receive_money'=>0.00,'total_money'=>0.00];
        $ret = http_web($this->api_url['default_account'])->send($params);
        if(isset($ret['code']) && $ret['code']===0 && isset($ret['data'][0])){
            $data['account'] = $ret['data'][0];
        } 
       
    
        
        $ret = http_web($this->api_url['account_sum'])->send($params);
     
        if(isset($ret['code']) && $ret['code']===0 && $ret['data']){
            $data = array_merge($data,$ret['data']);
        } 
      
        
        $ret = http_web($this->api_url['last_login'])->send(['user_id'=>session('user_id')]);
        if(isset($ret['code']) && $ret['code']===0 && $ret['data']){
            $data['last_login_time']=$ret['data'];
        }
        return view('web.user.seller.ejbao', $data);
    }
    
    public function create()
    { 
        
    }
    
    public function store(Request $request){
        if(!session('user_id')){
            return ['code'=>1,'message'=>"请先登录"];
        }
        //验证部分
        $data = $this->form();
 
        $rec_account_id = $data['rec_account_id'];
        $this->shop_id = session('user_shop_id');
        $this->user_id = session('user_id');
        
        $data['shop_id'] = $this->shop_id;
        $data['user_id'] = $this->user_id;
        $data['account_type'] = 1;
        $data['czbank_no'] = " ";
//         $data['account_icon']=" ";
//         $data['account_en'] = " ";
//         $data['bank_name'] = " ";

        
//如果是个人银行卡的话 可以获取图标 企业银行卡获取不了 
//         $bank_name = "";
//         $bank_url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=".$data['bank_no'];
//         $bank_json = file_get_contents($bank_url);
//         $bank_check=false;
//         if($bank_json){
//             $bank_data=json_decode($bank_json,true);
//             if($bank_data['validated']=="1"){
//                 $bank_name= config('bank.'.$bank_data['bank']);
//                 if(!$bank_name){
//                     $bank_name="未知银行";
//                 }
//                 $data['bank_name'] = $bank_name;
//                 $data['account_icon']="https://apimg.alipay.com/combo.png?d=cashier&t=".$bank_data['bank'];
//                 $data['account_en'] =$bank_data['bank'];
//                 $bank_check = true;
//             }
//         }
//         if(!$bank_check){
//             return ['code'=>1,'message'=>"银行卡校验错误"];
//         }

        
        
        $czdata['accountNo'] = $data['bank_no'];
        $czdata['accountName'] = $data['account_name'];  
        $czdata['accountType'] = $data['account_type'];
        $czdata['mobile'] = $data['bank_phone'];
        $czdata['certType'] = 1;
        $czdata['certNo'] = $data['cert_no'];
        $czdata['accountBranchNo'] = $data['accountBranchNo'];
        $czdata['certNo'] = $data['cert_no'];
        $czdata['mobileCode'] =$data['bank_code'];
        $czdata['mobileSerial'] = $data['mobileSerial'];
        
        $ret = http_web($this->api_url['check_pay_pwd'])->send(['pay_pwd'=>$data['pay_password'],'id'=>session('user_id')]);

        if(!isset($ret['code']) || $ret['code'] != 0){
            return ['code'=>1,'message'=>"支付密码错误"];
        }
             
     
        $ret = czbank_request($this->api_url['czbank_bind_receipt'])->send($czdata);

        if(isset($ret['code']) && $ret['code'] == 0){
            $data['czbank_no'] = $ret['data']['receiptSerialNo'];
            $data['accout_status'] = 1;
            $ret = http_web($this->api_url['update'],[$rec_account_id])->send($data);
            if(isset($ret['code']) && $ret['code'] == 0 ){
                Redis::set("has_default_".$this->user_id,TRUE);
            }
            return $ret;
        }else{
            return ['code'=>1,'message'=>$ret['message']];
        }
    }

    public function show($id)
    {
        //请求详情
        //$product = http_web($this->api_url['show'], [$id])->send();
        dd($id);
    }

    public function edit($id)
    {
        
    }
    
    public function destroy($id)
    {
        $ret = http_web($this->api_url['destroy'], [$id])->send();
        return $ret;
    }
    
    public function sendsms(){
        $data = $this->request->all(['mobileMark']);
        $data['smsType'] = 9;
        $ret = czbank_request($this->api_url['czbank_send_sms'])->send($data);
        return $ret;
    }
    
    public function checkcard(){
        $data = $this->request->all(['bank_no']);
        $bank_name = "";
        $bank_url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=".$data['bank_no'];
        $bank_json = file_get_contents($bank_url);
        $bank_check=false;
        if($bank_json){
            $bank_data=json_decode($bank_json,true);
            if($bank_data['validated']=="1"){
                $bank_name= config('bank.'.$bank_data['bank']);
                if($bank_name){
                    return ['code'=>0,'message'=>$bank_name];
                }
            
            }
        }
        return ['code'=>1,'message'=>"银行卡错误"];
    }
    
    public function getbankcode(){
        $data = $this->request->all(['branchName','bankType','cityCode']);
        $ret = czbank_request($this->api_url['czbank_query_bank'])->send($data);
        return $ret;
    }
    
    public function checkpaypwd(){
        $data = $this->request->all(['pay_pwd']);
        $data['id']=session('user_id');
        $ret = http_web($this->api_url['check_pay_pwd'])->send($data);
        return $ret;
    }
    
    public function updateDefault(Request $request){
        $data = $this->request->all(['id','is_default']);
        //请求部分
        $ret = http_web($this->api_url['update_default'])->send($data);
        return $ret;
    }
    
     
    
    public function update($id)
    { }
    
    
    private function form()
    {
        $this->validate($this->request, [
            'bank_no'=> 'required|string',
            'pay_password'=> 'required|string',
            'bank_phone'=> 'required|string',
            'bank_code'=> 'required|string',
            'cert_no'=> 'required|string',
            'account_name'=> 'required|string',
            'mobileSerial'=> 'required|string',
            'accountBranchNo'=> 'required|string',
        ]);
        
        return $this->request->all(
            ['czbank_no', 'bank_no','pay_password','bank_phone','bank_code','cert_no','account_name','mobileSerial','accountBranchNo','rec_account_id','bank_name',
        ]);
    }
}